package com.weijin.springbootbase.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weijin.springbootbase.common.PageResult;
import com.weijin.springbootbase.common.Result;
import com.weijin.springbootbase.mapper.UserMapper;
import com.weijin.springbootbase.pojo.User;
import com.weijin.springbootbase.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @Author WeiJin
 * @Version 1.0
 * @Date 2025/3/11 10:57
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisTemplate<String, User> redisTemplate;
//    @Resource
//    private ObjectMapper objectMapper;

    @Override
    public Result<List<User>> queryAll() {
        List<User> users = userMapper.selectAll();

        return !users.isEmpty() ? Result.success("查询成功", users) : Result.success("暂无任何数据");
    }

    @Override
    public Result<String> addUser(User user) {
        if (user.getAge() <= 0 || user.getAge() > 120) {
            return Result.failed("添加失败，非法年龄");
        }
        int inserted = userMapper.insertOne(user);
        return inserted > 0 ? Result.success("添加成功") : Result.failed("添加失败");
    }

    @Override
    public Result<String> deleteById(Integer id) {
        int deleted = userMapper.deleteById(id);
        return deleted > 0 ? Result.success("删除成功") : Result.failed("删除失败");
    }

    @Override
    public Result<String> modifyUser(User user) {
        int updated = userMapper.conditionUpdateById(user);
        return updated > 0 ? Result.success("修改成功") : Result.failed("修改失败");
    }

    @Override
    public Result<User> queryById(Integer id) {
        User user = userMapper.selectById(id);
        return Objects.isNull(user) ? Result.failed("用户不存在") : Result.success("查询成功", user);
    }

    @Override
    public Result<String> deleteBatchIds(Integer[] ids) {
        int i = userMapper.deleteBatchIds(ids);
        return i > 0 ? Result.success("删除成功") : Result.failed("删除失败");
    }

    @Override
    public Result<PageResult<User>> queryPage(Integer pageNum, Integer pageSize) {
        PageResult<User> userPageResult = new PageResult<>(pageNum, pageSize);
        //获取总记录数
        long count = userMapper.selectCount();
        userPageResult.setTotal(count);
        //计算总页数
        int num = (int) (count / pageSize);
        if (count % pageSize != 0) {
            num = num + 1;
        }
        userPageResult.setTotalPage(num);
        if (pageNum > num) {
            return Result.failed("页码超出总页数");
        }
        //构建起始数据行
        int current = (pageNum - 1) * pageSize;

        List<User> list = userMapper.selectLimit(current, pageSize);
        userPageResult.setList(list);

//        使用pagehelper插件做分页
//        PageHelper.startPage(pageNum,pageSize);
//        List<User> list = userMapper.selectAll();
//        PageInfo<User> userPageInfo = new PageInfo<>(list);

        return Result.success("查询成功", userPageResult);
    }

    @Override
    public Result<User> queryRedisById(Integer id) {
       User user = redisTemplate.opsForValue().get("user-id:" + id);
        if (Objects.isNull(user)) {
            user = userMapper.selectById(id);
            if (Objects.isNull(user)){
                return Result.failed("该用户不存在");
            }
            redisTemplate.opsForValue().set("user-id:" + id, user);
        }
        return Result.success("查询成功", user);
    }
}
